package com.huaxonline.boot.log;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * 日志配置属性类 - 简化版
 * 
 * @author zhaoshulei
 * @since 2024-05-22
 */
@ConfigurationProperties(prefix = "huaxonline.log")
public class LogProperties {

    /** 是否启用日志切面 */
    private boolean enabled = true;

    /** 默认采样率 [0.0, 1.0] */
    private double sampleRate = 1.0D;

    /** 全局脱敏字段 */
    private String[] maskFields = new String[0];

    /** 链路追踪ID字段名 */
    private String traceIdField = "traceId";

    /** 链路追踪ID请求头字段名 */
    private String traceIdHeader = "X-Trace-Id";

    /** 是否记录请求参数 */
    private boolean logRequestParams = true;

    /** 是否记录响应结果 */
    private boolean logResponseResult = true;

    /** 是否记录异常堆栈 */
    private boolean logExceptionStack = true;

    /** 脱敏替换字符 */
    private String maskReplacement = "***";

    /** 是否只记录业务Controller */
    private boolean onlyBusinessControllers = true;

    /** 业务Controller包路径（多个用逗号分隔） */
    private String[] businessPackages = {"com.huaxonline.boot"};

    /** 排除的URL模式（支持Ant风格） */
    private String[] excludeUrlPatterns = {
        "/actuator/**",
        "/swagger-ui/**",
        "/swagger-resources/**",
        "/v2/api-docs/**",
        "/v3/api-docs/**",
        "/webjars/**",
        "/favicon.ico",
        "/error"
    };

    /** 排除的Controller类名模式 */
    private String[] excludeControllerPatterns = {
        "*ActuatorController",
        "*SwaggerController",
        "*ErrorController"
    };

    /** 最小响应时间阈值（毫秒），超过此时间才记录日志 */
    private long minResponseTimeThreshold = 0L;

    /** 日志文件路径 */
    private String path = "./log";

    /** 日志备份路径 */
    private String backupPath = "./log/backup";

    /** 单个日志文件最大大小 */
    private String maxSize = "100MB";

    /** 日志文件保留天数 */
    private int maxHistory = 7;

    /** 日志文件总大小限制 */
    private String totalSizeCap = "10GB";

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public double getSampleRate() {
        return sampleRate;
    }

    public void setSampleRate(double sampleRate) {
        this.sampleRate = sampleRate;
    }

    public String[] getMaskFields() {
        return maskFields;
    }

    public void setMaskFields(String[] maskFields) {
        this.maskFields = maskFields;
    }

    public String getTraceIdField() {
        return traceIdField;
    }

    public void setTraceIdField(String traceIdField) {
        this.traceIdField = traceIdField;
    }

    public String getTraceIdHeader() {
        return traceIdHeader;
    }

    public void setTraceIdHeader(String traceIdHeader) {
        this.traceIdHeader = traceIdHeader;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getBackupPath() {
        return backupPath;
    }

    public void setBackupPath(String backupPath) {
        this.backupPath = backupPath;
    }

    public String getMaxSize() {
        return maxSize;
    }

    public void setMaxSize(String maxSize) {
        this.maxSize = maxSize;
    }

    public int getMaxHistory() {
        return maxHistory;
    }

    public void setMaxHistory(int maxHistory) {
        this.maxHistory = maxHistory;
    }

    public String getTotalSizeCap() {
        return totalSizeCap;
    }

    public void setTotalSizeCap(String totalSizeCap) {
        this.totalSizeCap = totalSizeCap;
    }

    public boolean isLogRequestParams() {
        return logRequestParams;
    }

    public void setLogRequestParams(boolean logRequestParams) {
        this.logRequestParams = logRequestParams;
    }

    public boolean isLogResponseResult() {
        return logResponseResult;
    }

    public void setLogResponseResult(boolean logResponseResult) {
        this.logResponseResult = logResponseResult;
    }

    public boolean isLogExceptionStack() {
        return logExceptionStack;
    }

    public void setLogExceptionStack(boolean logExceptionStack) {
        this.logExceptionStack = logExceptionStack;
    }

    public String getMaskReplacement() {
        return maskReplacement;
    }

    public void setMaskReplacement(String maskReplacement) {
        this.maskReplacement = maskReplacement;
    }

    public boolean isOnlyBusinessControllers() {
        return onlyBusinessControllers;
    }

    public void setOnlyBusinessControllers(boolean onlyBusinessControllers) {
        this.onlyBusinessControllers = onlyBusinessControllers;
    }

    public String[] getBusinessPackages() {
        return businessPackages;
    }

    public void setBusinessPackages(String[] businessPackages) {
        this.businessPackages = businessPackages;
    }

    public String[] getExcludeUrlPatterns() {
        return excludeUrlPatterns;
    }

    public void setExcludeUrlPatterns(String[] excludeUrlPatterns) {
        this.excludeUrlPatterns = excludeUrlPatterns;
    }

    public String[] getExcludeControllerPatterns() {
        return excludeControllerPatterns;
    }

    public void setExcludeControllerPatterns(String[] excludeControllerPatterns) {
        this.excludeControllerPatterns = excludeControllerPatterns;
    }

    public long getMinResponseTimeThreshold() {
        return minResponseTimeThreshold;
    }

    public void setMinResponseTimeThreshold(long minResponseTimeThreshold) {
        this.minResponseTimeThreshold = minResponseTimeThreshold;
    }

    /**
     * 获取脱敏字段的Set集合
     */
    public Set<String> getMaskFieldsSet() {
        return new HashSet<>(Arrays.asList(maskFields));
    }

    /**
     * 获取业务包路径的Set集合
     */
    public Set<String> getBusinessPackagesSet() {
        return new HashSet<>(Arrays.asList(businessPackages));
    }

    /**
     * 获取排除URL模式的Set集合
     */
    public Set<String> getExcludeUrlPatternsSet() {
        return new HashSet<>(Arrays.asList(excludeUrlPatterns));
    }

    /**
     * 获取排除Controller模式的Set集合
     */
    public Set<String> getExcludeControllerPatternsSet() {
        return new HashSet<>(Arrays.asList(excludeControllerPatterns));
    }
}

